﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace EnglishRegister.Entities
{
    public class BaseModel<T> where T : class
    {
        #region **Common Functions**
        public void Insert()
        {
            try
            {
                using (EnglishRegisterContext db = new EnglishRegisterContext())
                {
                    db.Insert<T>(this as T);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public void Delete()
        {
            try
            {
                using (EnglishRegisterContext db = new EnglishRegisterContext())
                {
                    db.Delete<T>(this as T);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public void Update()
        {
            try
            {
                EnglishRegisterContext db = new EnglishRegisterContext();
                db.Update<T>(this as T);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public static System.Data.Linq.Table<T> GetTable()
        {
            try
            {
                EnglishRegisterContext db = new EnglishRegisterContext();
                return db.GetTable<T>();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public static List<T> GetAll()
        {
            try
            {
                EnglishRegisterContext db = new EnglishRegisterContext();
                return db.GetTable<T>().ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        #endregion
    }
}
